< previous page page_376 next page >

Page 376
Polymorphism
Literally translated from Greek, polymorphism means multifaceted or many faced. What this means in the programming world is that similar classes can contain properties and methods of the same name, but they have different behaviors. For example, two objects of different class types may have Paint methods, but what they paint is different for each object. Heres another example: You have a Customer object and an Invoice object, and each has a Print method. Obviously, the Print method does different things for the Customer object than it does for the Invoice object.
Most likely, youve already used polymorphism and you may not have even realized it. If you create two forms, you have created two separate classes. When you display each form, you use the method Show. This method is implemented in each form, but it displays each form differently because each form is designed differently.
OOP purists would argue that to truly have polymorphism, you must have inheritance. Because Visual Basic implements interfaces and not inheritance, they would be right. However, polymorphism can still exist, just by defining a standard set of properties and methods that are used across all your own objects.
Constructors
A constructor is a procedure thats automatically invoked when an object is first instantiated. Constructors contain code to perform an objects initialization. Each class created in Visual Basic has a Class_Initialize() procedure as its constructor.
The best example of a constructor involves what happens when you insert a new form into a project. The form appears in the design environment with a default size, border style, colors, and so on. This does not happen by magicsome programmer wrote the constructor event for a form class that initializes all the properties for that form object.
Consider an example of a class module that opens a handle to a file on disk. To do this, it must ask the operating system for the file handle. The operating system allocates that file handle in memory and passes it to Visual Basic. If you forget to release that file handle, youve stolen resources from the operating system. In the Class_Initialize procedure, you grab the file handle, and in the destructor, you release that file handle.

 
< previous page page_376 next page >

If you like this book, buy it!